/*
 * Copyright (c) 2010 Marc Poppleton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.marcpoppleton.strawberry.provider;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;


public class Unit implements Parcelable, BaseColumns{
	
    public static final Uri CONTENT_URI = Uri.parse("content://strawberry/units");
    
    public static final String[] PROJECTION_IDS_AND_ALL = new String[] {
        Unit._ID,Unit.VALUE,Coc.SORT_NAME
    };    

    public static final String DEFAULT_SORT_ORDER = "_id ASC";

    public static final String VALUE = "value";
    public static final String SORT_NAME = "_id";
    
    public static final String PARCEL_NAME = "parcel";
	
    private String mName = "";
    private Float mValue = 0.000F;
    
    
    public Unit(){}
    
    private Unit(Parcel in) {
        mName = in.readString();
        mValue = in.readFloat();
    }    
  
    public static Unit fromCursor(Cursor c) {
    	final Unit unit = new Unit();

        final int iNameIndex = c.getColumnIndex(_ID);
        final int iValueIndex = c.getColumnIndex(VALUE);

        if (iNameIndex!=-1) unit.setName(c.getString(iNameIndex));
        if (iValueIndex!=-1) unit.setValue(c.getFloat(iValueIndex));
        
        return unit;
    }    
    
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mName);
        dest.writeFloat(mValue);
    }     
    
    public ContentValues getContentValues() {
        final ContentValues values = new ContentValues();

        values.put(_ID, mName);
        values.put(VALUE, mValue);

        return values;
    }    
    
	public String getName() {
		return mName;
	}
	public void setName(String name) {
		mName = name;
	}
	public Float getValue() {
		return mValue;
	}
	public void setValue(Float value) {
		mValue = value;
	}
    public int describeContents() {
        return 0;
    }
    
    public String toString(){
    	return getName() + " (" + getValue() + ")";
    }
    
    public static final Creator<Unit> CREATOR = new Creator<Unit>() {
        public Unit createFromParcel(Parcel in) {
            return new Unit(in);
        }

        public Unit[] newArray(int size) {
            return new Unit[size];
        }
    };	

    
}